package com.cy.stone.system.adapter;

import cn.dev33.satoken.stp.StpUtil;
import com.cy.stone.system.application.AuthApi;
import com.cy.stone.system.domain.clientobject.auth.LoginCO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 认证接口
 * @author Wings
 * @since 2023-12-10
 */
@RestController
@RequestMapping("/auth/")
@RequiredArgsConstructor
@Tag(name = "认证服务适配器")
public class AuthController {

    private final AuthApi authApi;

    /**
     * 登录认证
     * @param loginCO 登录入参
     * @return token
     * @author Wings
     * @since 2023-12-10
     */
    @PostMapping("login")
    @Operation(summary = "登录")
    public String login(@RequestBody @Valid LoginCO loginCO) {
        authApi.auth(loginCO);
        StpUtil.login(loginCO.getUserName());
        return StpUtil.getTokenValue();
    }
}
